package com.weishi.framework.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.config.GlobalConfig.DbConfig;
import com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.io.VFS;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * 数据源配置
 */
@Configuration
@MapperScan(basePackages = "com.weishi.project.**.mapper")
public class DataSourceConfig {

    /**
     * druid数据库连接池配置
     */
    @Bean(initMethod = "init", destroyMethod = "close")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
        DruidDataSource defaultDataSource = new DruidDataSource();
        return defaultDataSource;
    }

    /**
     * mybatis-plus配置
     */
    @Bean
    public MybatisConfiguration mybatisConfiguration() {
        MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
        // 全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        // 数据库配置
        DbConfig dbConfig = new DbConfig();
        // 主键自增
        dbConfig.setIdType(IdType.AUTO);
        globalConfig.setDbConfig(dbConfig);
        // 不打印mybatis-plus的banner(花里胡哨)
        globalConfig.setBanner(false);
        mybatisConfiguration.setGlobalConfig(globalConfig);
        // mybatis下划线转驼峰
        mybatisConfiguration.setMapUnderscoreToCamelCase(true);
        // map结果集下划线转驼峰
        mybatisConfiguration.setObjectWrapperFactory(new MybatisMapWrapperFactory());
        // 部分数据库不识别默认的NULL类型，需要配置该属性
        mybatisConfiguration.setJdbcTypeForNull(JdbcType.NULL);
        return mybatisConfiguration;
    }

    /**
     * 数据库连接工厂
     */
    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource, MybatisConfiguration mybatisConfiguration, ResourcePatternResolver resourcePatternResolver) throws Exception {
        MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
        // 设置数据源
        factoryBean.setDataSource(dataSource);
        // mybatis配置
        factoryBean.setConfiguration(mybatisConfiguration);
        // mybatis-plus全局配置
        factoryBean.setGlobalConfig(mybatisConfiguration.getGlobalConfig());
        // 支持springboot打包后识别实体类的别名
        VFS.addImplClass(SpringBootVFS.class);
        // 实体别名的包路径
        factoryBean.setTypeAliasesPackage("com.weishi.project");
        // 设置xml扫描路径
        factoryBean.setMapperLocations(resourcePatternResolver.getResources("classpath*:mybatis/*/*.xml"));
        // page-helper分页插件
        PageInterceptor pageInterceptor = new PageInterceptor();
        pageInterceptor.setProperties(new Properties());
        // 打印sql记录的插件
        SqlRecordInterceptor sqlRecordInterceptor = new SqlRecordInterceptor();
        // 配置上面的插件
        factoryBean.setPlugins(pageInterceptor, sqlRecordInterceptor);
        return factoryBean.getObject();
    }

    /**
     * 事务管理器
     */
    @Bean
    public DataSourceTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

}